package com.wdh.kjs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.activity.FeedBackActivity;
import com.wdh.kjs.activity.LoginActivity;
import com.wdh.kjs.fragment.FinanceFragment;
import com.wdh.kjs.fragment.HomePageFragment;
import com.wdh.kjs.fragment.InvestmentFragment;
import com.wdh.kjs.fragment.KeFuFragment;
import com.wdh.kjs.fragment.MyAccountFragment;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.http.response.NotifyDownload;
import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.util.ToastUtils;

public class MainActivity extends FragmentActivity {

    public static FragmentTabHost mFragmentTabHost;
    private String[] tabTags = {"tab_HomePage", "tab_investment","tab_finance", "tab_myAccountFragment","tab_KeFuFragment"};
    private Class[] tabFragments = {HomePageFragment.class, InvestmentFragment.class, FinanceFragment.class, MyAccountFragment.class, KeFuFragment.class};
    private String url;
    private PackageInfo packInfo;
    private String[] tabTitles = {"首页", "投资","融资", "账户","客服"};
    private int[] tabIcons = {R.drawable.tab_chainwinfinance, R.drawable.tab_investment,R.drawable.tab_finance,R.drawable.tab_myaccount,R.drawable.footerkefu};
    private JsonModel jsonModel;
    public static LinearLayout llTab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Update();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initView() {
        mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realTabContent);

    for (int i = 0; i < tabFragments.length; i++) {
        TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(tabTags[i]);
        tabSpec.setIndicator(getTabView(i));
        mFragmentTabHost.addTab(tabSpec, tabFragments[i], null);
    }


        mFragmentTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
    }

    public View getTabView(int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.view_tab_main, null);
         llTab = (LinearLayout) view.findViewById(R.id.ll_mainTab);
        ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_mainTabIcon);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_mainTabTitle);
        ivIcon.setImageResource(tabIcons[i]);
        tvTitle.setText(tabTitles[i]);
        tvTitle.setTextSize(10);
        return view;
    }
    private void Update(){



        new HttpUtils().send(HttpRequest.HttpMethod.POST, Httpurl.URL_UPDATE, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();

                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);
                if (jsonModel.isSuccess()) {
                    url = Httpurl.HOST + jsonModel.getAndroidVersionInfo().getAndroidUrl();


                    String versionCode = getVersionCode(); // 当前本地版本
                    String currentCode = jsonModel.getAndroidVersionInfo().getAndroidVersionNum(); // 服务器发布版本
                    if (Double.parseDouble(versionCode) >= Double.parseDouble(currentCode)) {
                        Toast.makeText(MainActivity.this,
                                "当前版本为" + versionCode + "，已是最新!", Toast.LENGTH_SHORT)
                                .show();
                    } else {
                        new AlertDialog.Builder(
                                MainActivity.this)
                                .setTitle("新版本")
                                .setMessage(
                                        "当前最新版本为"
                                                + jsonModel.getAndroidVersionInfo().getAndroidVersionNum()
                                                + "\n更新内容包括:" + jsonModel.getAndroidVersionInfo().getAndroidVersionInfo() + "\n建议更新")
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                NotifyDownload down = new NotifyDownload(
                                                        MainActivity.this);
                                                down.startDownload(
                                                        MainActivity.this,
                                                        url);
                                            }
                                        })
                                .setNegativeButton("取消", null)
                                .create().show();
                    }
                    final SharedPreferences[] preferences = {getSharedPreferences("Version", MODE_PRIVATE)};
                    preferences[0] = getSharedPreferences("Version", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = preferences[0].edit();

                    editor.putString("version", jsonModel.getAndroidVersionInfo().getAndroidVersionNum());

                    editor.commit();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {


                ToastUtils.showShort(MainActivity.this, "网络异常!");

            }
        });

    }
    /*
        * 获取当前程序的版本号
        */
    private String getVersionCode() {
        // 获取packagemanager的实例
        PackageManager packageManager =MainActivity.this
                .getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息

        try {
            packInfo = packageManager.getPackageInfo(
                    MainActivity.this.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return packInfo.versionName;

    }

}
